import json
import sys

import grpc
from entity import migrate_pb2
from grpc_service import migrate_pb2_grpc
from db import factory
from util import file_util


def run():
    """
    "10.8.0.2:3306/ks-tequila-v2?username=root&password=Luck158," "10.8.0.2:3306/ks-tequila-v3?username=root&password=Luck158," "select * from task_record"
    "10.8.0.2:8086/bfbd2022?username=root&password=BICI123456" "10.8.0.2:8086/bfbd2023?username=root&password=BICI123457" "/home/lifeng/WORK/code/python/middleware/static/influx_demo.sql"
    :return:
    """
    args = sys.argv
    origin_url = args[1]
    dest_url = args[2]
    request = args[3] # 可能为路径
    db = args[4]

    if "/" in request:
        request = file_util.read_file(request)
    else:
        request = request.split(",")

    try:
        channel = grpc.insecure_channel('localhost:8088')  # 连接上gRPC服务端
        stub = migrate_pb2_grpc.MigrateServiceStub(channel)

        factory_clz = factory.get_migrate_client_factory(db)
        service = factory_clz(origin_url,dest_url,request,500)
        # 解析请求中的sql
        service.parse_sql()
        service.show_create_table(stub,db)
        # 开始分页
        service.start_sharding()
        # 建表
        # 开始建议数据
        service.migrate(stub,db)


    except Exception as e:
        print(e)


if __name__ == "__main__":
    run()
